#include <iostream>
using namespace std;

class A {
    public:
        A(int a):x(a) { //construct with int
            cout << "调用含参(int)构造函数" << endl;
        }
        A(int a, int b) {
            x = a;
            cout << "调用含参(int, int)构造函数" << endl;
        }
        A() {
            x = 0;
            cout << "调用无参构造函数" << endl;
        }
        A(A const &obj) {
            x = obj.x;
            cout << "调用复制构造函数" << endl;
        }
        A &operator=(const A &obj) { 
            cout << "调用赋值(const A&)函数" << endl;
            if(this != &obj) x = obj.x;
            return *this;
        }
#ifdef ASBI // assign with int
        A &operator=(int a) {
            x = a;
            cout << "调用赋值(int)函数" << endl;
            return *this
        }
#endif //ASBI
        
        ~A() {
            cout << "调用析构函数" << endl;
        }
    private:
        int x;
};

int main() {
    A a; //不能加括号: A a(), 否则编译器翻译为声明了一个返回类型为A的无参函数a
    A b(1);
    cout << "A c = A(1)" << endl;
    A c = A(1); //编译器优化为: A c(1), 因此只调用一次构造函数
        //-fno-elide-constructors: A c(A(1)), 先含参数构造再复制构造
    cout << "A f = 1" << endl;
    A f = 1; // ==> A f = A(1) ==> A f(1); 因此只调用含参构造函数
    cout << "spacer1" << endl;
    A d = c; //拷贝构造，这里等号不代表赋值

    a = b; //赋值(const A&)
    cout << "f = 1" << endl;
    f = 1; 
        //优先匹配赋值函数(A&operator=(int))，否则先含参再复制构造(这里是赋值，
        //不能直接优化为含参构造): f = A(1);
    cout << "f = A(1)" << endl;
    f = A(1); //构造函数---赋值函数---析构函数, 赋值不会优化
        
    cout << "finish" << endl;
    
    return 0;
}
